package com.one.group.user.controller;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.one.group.enums.ErrorCode;
import com.one.group.model.dto.SpHouseRentalAddDto;
import com.one.group.model.dto.SpHouseRentalQueryDto;
import com.one.group.model.entity.*;
import com.one.group.model.vo.SpHouseRentalListVo;
import com.one.group.model.vo.SpHouseVo;
import com.one.group.result.Result;
import com.one.group.user.service.HouseCacheService;
import com.one.group.utils.ResultUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/house")
@Tag(name = "房屋相关接口")
public class HouseController {
    @Autowired
    private HouseCacheService houseCacheService;

    @GetMapping("/getBuildingType")
    @Operation(summary = "根据小区id获取建筑类型信息")
    public Result<List<SpBuildingtype>> findBuildingTypeByCellId(Long cellId) {
        List<SpBuildingtype> buildingtypeList = houseCacheService.getBuildingTypeByCellId(cellId);
        return ResultUtil.success(buildingtypeList);
    }
    @GetMapping("/getBuilding")
    @Operation(summary = "根据建筑类型id获取建筑信息")
    public Result<List<SpBuilding>> findBuildingByType(Long BuildingTypeId) {
        List<SpBuilding> buildingList = houseCacheService.getBuildingByType(BuildingTypeId);
        return ResultUtil.success(buildingList);
    }
    @GetMapping("/getUnit")
    @Operation(summary = "根据建筑id获取单元信息")
    public Result<List<SpUnit>> findUnitByBuilding(Long BuildingId) {
        List<SpUnit> unitList = houseCacheService.getUnitByBuilding(BuildingId);
        return ResultUtil.success(unitList);
    }
    @GetMapping("/getHouse")
    @Operation(summary = "根据单元id获取房屋信息")
    public Result<List<SpHouse>> findHouseByUnitId(Long UnitId) {
        List<SpHouse> houseList = houseCacheService.getHouseByUnitId(UnitId);
        return ResultUtil.success(houseList);
    }
    @GetMapping("getHouseByOwnerId")
    @Operation(summary = "根据用户id获取房屋信息")
    public Result<List<SpHouse>> findHouseByOwnerId(Long ownerId) {
        List<SpHouse> houseList = houseCacheService.getHouseByOwnerId(ownerId);
        return ResultUtil.success(houseList);
    }
    @GetMapping("/detail")
    @Operation(summary = "获取房屋详情")
    public Result<SpHouseVo> findHouseDetail(Long houseId) {
        SpHouseVo house = houseCacheService.getHouseById(houseId);
        return ResultUtil.success(house);
    }
    @PostMapping("/setHouseRent")
    @Operation(summary = "设置房屋出租")
    public Result setHouseRent(@RequestBody SpHouseRentalAddDto spHouseRentalAddDto) {
        if (houseCacheService.rentHouse(spHouseRentalAddDto)) {
            return ResultUtil.success();
        } else {
            return ResultUtil.error(ErrorCode.OPERATION_ERROR);
        }
    }
    @GetMapping("/chackHouseIsRent")
    @Operation(summary = "检查房屋是否出租")
    public Result<SpHouseRental> chackHouseIsRent(Long houseId) {
        return ResultUtil.success(houseCacheService.chackHouseIsRent(houseId));
    }
    @PostMapping("/queryList")
    @Operation(summary = "查询房屋租赁信息")
    public Result queryList(@RequestBody SpHouseRentalQueryDto spHouseRentalQueryDto){
        PageHelper.startPage(spHouseRentalQueryDto.getPage(),spHouseRentalQueryDto.getLimit());
        List<SpHouseRentalListVo> spHouseRentalListVoList = houseCacheService.listSpHouseRentals(spHouseRentalQueryDto);
        PageInfo<SpHouseRentalListVo> pageInfo = new PageInfo<>(spHouseRentalListVoList);
        return ResultUtil.success(pageInfo);
    }
}
